#!/bin/bash

FIND_EXT=$(find /userdata/system -maxdepth 1 -name "extension_*.tar.gz" | head -1)

FIND_OVERLAY=
test -e "/boot/boot/overlay" && FIND_OVERLAY=1

FIND_CUSTOMSH=
test -e "/userdata/system/custom.sh" && FIND_CUSTOMSH=1

FIND_CUSTOMESSYSTEMS=
test -e "/userdata/system/configs/emulationstation/es_systems.cfg" && FIND_CUSTOMESSYSTEMS=1

FIND_CUSTOMESFEATURES=
test -e "/userdata/system/configs/emulationstation/es_features.cfg" && FIND_CUSTOMESFEATURES=1

FIND_CUSTOMSERVICES=$(batocera-services list user | grep '\*$' | head -1)

FIND_PRO=
test -e "/userdata/system/pro" && FIND_PRO=1

FIND_ADDONS=
test -e "/userdata/system/add-ons" && FIND_ADDONS=1

EXTRA=
test -n "${FIND_ADDONS}"           && EXTRA=${EXTRA}a
test -n "${FIND_CUSTOMSH}"         && EXTRA=${EXTRA}c
test -n "${FIND_EXT}"              && EXTRA=${EXTRA}e
test -n "${FIND_CUSTOMESFEATURES}" && EXTRA=${EXTRA}f
test -n "${FIND_OVERLAY}"          && EXTRA=${EXTRA}o
test -n "${FIND_PRO}"              && EXTRA=${EXTRA}p
test -n "${FIND_CUSTOMESSYSTEMS}"  && EXTRA=${EXTRA}s
test -n "${FIND_CUSTOMSERVICES}"   && EXTRA=${EXTRA}u

if test "${1}" = "--extra"
then
    if test -z "${EXTRA}"
    then
	echo "none"
    else
	echo "${EXTRA}"
    fi
else
    cat /usr/share/batocera/batocera.version | sed -e s+"^\([0-9]*\)"+"\1${EXTRA}"+
fi
